package com.sduept.nwld.secondaryequipment.evaluation.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.protection.model.ProtectChannelAlarm;
import com.sduept.bigdata.fault.protection.model.ProtectionActionInfoDTO;
import com.sduept.bigdata.fault.protection.service.ProtectChannelAlarmQueryManager;
import com.sduept.bigdata.fault.protection.service.ProtectionActionQueryManager;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectResponseTime;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
@RestController
@RequestMapping("/protect")
public class ProtectService {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ProtectionActionQueryManager pa;
	@Autowired
	private ProtectChannelAlarmQueryManager pca;
	@Autowired
	private CimManager cm;

	/**
	 * 根据线路cimId获取该线路上所有的保护
	 * 
	 * @param lineid
	 * @return [[mid,name,pt_id]]
	 */

	@GetMapping("/getProtects/{lineid}")
	public String getProtects(@PathVariable("lineid") String lineid) {
//		String sql = "select mRID,mProtectName,pt_id from t_bx_protection_equipment t ,t_bx_aclinesegment t2 where t2.line_cim_id = '"
//				+ lineid + "' and t.mPrimdevID = t2.cim_mid ";
//		List<?> obs = dao.nativeQuery(sql);
//		for (Object o : obs) {
//			Object[] arr = (Object[]) o;
//			String str = arr[1].toString();
//			int pos = str.indexOf("线");
//			str = str.substring(pos + 1);
//			arr[1] = str;
//		}
//		return JSONObject.toJSONString(obs);
		List<ProtectionEquipment> list = cm.getProtectionEquipmentsByAclineId(lineid);
		List<Object[]> objs = new ArrayList<Object[]>();
		for (ProtectionEquipment equipment : list) {
			Object[] obj = new Object[3];
			obj[0] = equipment.getId();
			obj[1] = equipment.getName();
			obj[2] = equipment.getPtId();
			objs.add(obj);
		}
		return JSONObject.toJSONString(objs);
	}


	@GetMapping("/getProtectActsByLineId/{lineid}")
	public String getProtectActsByLineId(@PathVariable("lineid") String lineid) {
//		String sql = "select t2.mRID,t2.mProtectName,t.timeLag,t.actTime from t_faultwave_line_protect_act t ,t_bx_protection_equipment t2,t_bx_aclinesegment t3 "
//				+ "where t.deviceId = t2.mRID and t2.mPrimdevID = t3.cim_mid and  t3.line_cim_id='"
//				+ lineid + "'";
//		List<?> obs = dao.nativeQuery(sql);
//		for (Object o : obs) {
//			Object[] arr = (Object[]) o;
//			String str = arr[1].toString();
//			int pos = str.indexOf("线");
//			str = str.substring(pos + 1);
//			arr[1] = str;
//		}
//		return JSONObject.toJSONString(obs);
		List<ProtectionActionInfoDTO> obs = pa.getProtectionActionTimeDtoByLineId(new String[] { lineid }, null, null);
		List<Object[]> objs = new ArrayList<Object[]>();
		for (ProtectionActionInfoDTO equipment : obs) {
			Object[] obj = new Object[4];
			obj[0] = equipment.getProtectionId();
			obj[1] = equipment.getProtectionName();
			obj[2] = equipment.getDistance0MomentsTime();
			obj[3] = equipment.getFaultTime();
			objs.add(obj);
		}
		return JSONObject.toJSONString(obs);
	}

	/**
	 * 保护动作时间
	 * 
	 * @param mrid @return[[timeLag,deviceId,deviceName]]
	 */

	@GetMapping("/getProtectActionTimesByVoltage/{voltage}")
	public String getProtectActionTimesByVoltage(@PathVariable("voltage") String voltage) {
		// float average = 12.65f;

		Random random = new Random();
		List<Float> rtn = new ArrayList<>();
		for (int i = 0; i < 200; i++) {
			rtn.add(new Float(random.nextInt(3) + 10 + random.nextFloat()));
		}
		for (int i = 0; i < 20; i++) {
			rtn.add(new Float(random.nextInt(3) + 7 + random.nextFloat()));
		}
		for (int i = 0; i < 20; i++) {
			rtn.add(new Float(random.nextInt(13) + 27 + random.nextFloat()));
		}

		return JSONObject.toJSONString(rtn);
	}


	@GetMapping("/getProtectAlarmsByLineId/{lineid}")
	public String getProtectAlarmsByLineId(@PathVariable("lineid") String lineid) {
//		String sql = "select t2.mProtectName,t.level,t.trigger_time,t.alarm_channel,t.name  from t_txbh_alarm t, t_bx_protection_equipment t2,t_bx_aclinesegment t3 "
//				+ "where t2.mPrimdevID = t3.cim_mid and  t3.line_cim_id='"
//				+ lineid + "' and t.bh_id = t2.pt_id";
//		List<?> obs = dao.nativeQuery(sql);
//		for (Object o : obs) {
//			Object[] arr = (Object[]) o;
//			String str = arr[0].toString();
//			int pos = str.indexOf("线");
//			str = str.substring(pos + 1);
//			arr[0] = str;
//
//			str = arr[3].toString();
//			pos = str.indexOf("线");
//			str = str.substring(pos + 1);
//			arr[3] = str;
//		}
		List<ProtectChannelAlarm> obs = pca.getUnRecoveryProtectChannelAlarmByLineId(new String[] { lineid }, null,
				null);
		return JSONObject.toJSONString(obs);
	}

	/**
	 * 保护告警信息
	 * 
	 * @param mrid 暂时用pt_id @return[[ bh_id,level,trigger_time,name,alarm_channel]]
	 */

	@GetMapping("/getProtectChanelAlarms/{mrid}")
	public String getProtectChanelAlarms(@PathVariable("mrid") String mrid) {
		String sql = "select bh_id,level,trigger_time,name,alarm_channel from t_txbh_alarm where bh_id='" + mrid + "'";
		List<?> obs = dao.nativeQuery(sql);
		return JSONObject.toJSONString(obs);
	}

	/**
	 * 保护厂家
	 * 
	 * @return [devManufacturer]
	 */

	@GetMapping("/getProtectCompanies")
	public String getProtectCompanies() {
		String sql = "select distinct devManufacturer from t_bx_protection_equipment";
		List<?> obs = dao.nativeQuery(sql);
		return JSONObject.toJSONString(obs);
	}

	/**
	 * 保护厂家和保护型号
	 * 
	 * @return [[devType,devManufacturer]]
	 */
	//
	// @GetMapping("/getProtectCompaniesTypes")
	public String getProtectCompaniesTypes() {
		String sql = "	select distinct devType,t.devManufacturer from t_bx_protection_equipment t where t.devManufacturer is not null";
		List<?> obs = dao.nativeQuery(sql);
		String s = JSONObject.toJSONString(obs);
		return s;
	}

	/**
	 * 保护厂家和保护型号
	 * 
	 * @return [[devType,devManufacturer]]
	 */

	@GetMapping("/getProtectCompaniesTypes")
	public String getProtectCompaniesTypes1() {
		String sql = "	select distinct t.protect_type,t.protect_manufacturer from t_count_protect_response_time t where t.protect_manufacturer is not null";
		List<?> obs = dao.nativeQuery(sql);
		String s = JSONObject.toJSONString(obs);
		return s;
	}

	/**
	 * 保护动作时间以及保护型号 返回所有的保护动作时间，以及保护的型号
	 * 
	 * @return [[timelag,devType]]
	 */
//
//	@GetMapping("/getProtectActAndType")
	public String getProtectActAndType() {
		Calendar calendar = Calendar.getInstance();
		// 获取最近四年动作时间
		Date endDate = calendar.getTime();
		// 四年之前1月1日
		calendar.set(calendar.get(Calendar.YEAR) - 3, 0, 1);
		Date startDate = calendar.getTime();
		List<ProtectionActionInfoDTO> pact = pa.getProtectionActionTimeDtoByDate(startDate, endDate);
		JSONArray jArr = new JSONArray();
		for (ProtectionActionInfoDTO p : pact) {
			List<String> list = new ArrayList<>();
			list.add(Integer.toString(p.getDistance0MomentsTime()));
			list.add(p.getDevType());
			jArr.add(list);
		}

		/*
		 * String sql =
		 * "select t.timeLag,t2.devType from t_faultwave_line_protect_act t,t_bx_protection_equipment t2 "
		 * + "where t.deviceId = t2.mRID"; List<?> obs = dao.nativeQuery(sql);
		 */
		return jArr.toString();
	}

	/**
	 * 保护动作时间以及保护型号
	 * 
	 * @return [[timelag,devType]]
	 */

	@GetMapping("/getProtectActAndType")
	public String getProtectActAndType1() {
		Calendar calendar = Calendar.getInstance();
		// 获取最近四年动作时间
		Date endDate = calendar.getTime();
		// 四年之前1月1日
		calendar.set(calendar.get(Calendar.YEAR) - 3, 0, 1);
		Date startDate = calendar.getTime();
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		List<ProtectResponseTime> lp = dao.findByCondition("fault_time>='" + dateFormater.format(startDate)
//				+ "' and fault_time<= '" + dateFormater.format(endDate) + "'", ProtectResponseTime.class);
		List<ProtectResponseTime> lp = dao.findByCondition(ProtectResponseTime.class,
				"fault_time>= ?  and fault_time<= ?", dateFormater.format(startDate), dateFormater.format(endDate));
		JSONArray jArr = new JSONArray();
		for (ProtectResponseTime p : lp) {
			List<String> list = new ArrayList<>();
			list.add(p.getActTime());
			list.add(p.getProtectType());
			jArr.add(list);
		}
		return jArr.toString();
	}

}
